﻿using System;
using System.Linq;
using System.Collections.Generic;
using NLite.Collections;
using NLite.Internal;

namespace NLite
{
    /// <summary>
    /// 监听管理器
    /// </summary>
    /// <typeparam name="TListner">监听器类型</typeparam>
    /// <remarks>观察者模式中的抽象主题角色</remarks>
    [Serializable]
    public class ListnerManager<TListner> : MarshalDisposable, IListnerManager<TListner>, IEnumerable<TListner>
        where TListner : IListener
    {
        private ICollection<TListner> Listners;

        /// <summary>
        /// 
        /// </summary>
        public ListnerManager()
        {
            Listners = new SyncList<TListner>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="listner"></param>
        protected virtual void OnAfterRegister(TListner listner)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="listner"></param>
        protected virtual void OnAfterUnRegister(TListner listner)
        {

        }

        /// <summary>
        /// 注册监听器
        /// </summary>
        /// <param name="Listners">监听器集合</param>
        public void Register(params TListner[] listners)
        {
            if (listners != null && listners.Length > 0)
                foreach (var item in listners)
                    if (!this.Listners.Contains(item))
                    {
                        this.Listners.Add(item);
                        OnAfterRegister(item);
                    }
        }

        /// <summary>
        /// 注销监听器
        /// </summary>
        /// <param name="listner">监听器集合</param>
        public void UnRegister(params TListner[] listners)
        {
            if (listners != null && listners.Length > 0)
                foreach (var item in listners)
                    if (this.Listners.Contains(item))
                    {
                        this.Listners.Remove(item);
                        OnAfterUnRegister(item);
                    }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        public void ForEach(Action<TListner> action)
        {
            if (action != null)
                foreach (var item in Listners)
                    if (item != null)
                        action(item);
        }
        /// <summary>
        /// 监听器数量
        /// </summary>
        public int Count
        {
            get { return Listners.Count; }
        }

        /// <summary>
        /// 得到迭代子对象
        /// </summary>
        public IEnumerator<TListner> GetEnumerator()
        {
            return Listners.GetEnumerator();
        }

        /// <summary>
        /// 得到迭代子对象
        /// </summary>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Listners.GetEnumerator();
        }

        protected override void Dispose(bool disposing)
        {
            Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            foreach (var item in Listners.ToArray())
            {
                var dis = item as IDisposable;
                if (dis != null)
                    dis.Dispose();
            }

            Listners.Clear();
        }

       
    }
}
